﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;

namespace Freshbox2._0.UI.Controllers
{
    public class StoreDicountController : BaseController<storediscount>
    {
        // APP端 店铺满减活动

        // GET: /StoreDicount/


        public override BLL.BaseBll<storediscount> bll
        {
            get
            {
                return new StoreDiscountBll();
            }
        }

       /// <summary>
        /// 暂时只是 店铺打折优惠 显示type == 1 
       /// </summary>
       /// <param name="page"></param>
       /// <param name="rows"></param>
       /// <param name="model"></param>
       /// <returns></returns>
        public ActionResult GetAll(int page, int rows, storediscount model)
        {
            //storeDiscountId storeId value type storeName startTime   engTime  status &&x.store.storeStatus==1
            if ( model.storeId== 0)
            {
                var list = bll.Search(x => x.type == 1 && x.store.storeStatus == 1).Select(x => new
                {
                    x.storeId,
                    x.storeDiscountId,
                    x.value,
                    type = x.type == 1 ? "店铺折扣" : x.type == 2 ? "单品折扣" : "其他",
                    x.store.storeName,
                    engTime = x.endTime,
                    startTime = x.startTime,
                    status = x.status == 1 ? "有效" : "无效",
                }).ToList();
                var list2 = list.ToPagedList(page, rows).ToList();
                return Json(new { total = list.Count, rows = list2 });
            }
            else
            {
                var list = bll.Search(x => x.type == 1 && x.store.storeStatus == 1).Where(x=>x.storeId==model.storeId).Select(x => new
                {
                    x.storeId,
                    x.storeDiscountId,
                    x.value,
                    type = x.type == 1 ? "店铺折扣" : x.type == 2 ? "单品折扣" : "其他",
                    x.store.storeName,
                    engTime = x.endTime,
                    startTime = x.startTime,
                    status = x.status == 1 ? "有效" : "无效",
                }).ToList();
                var  list2 = list.ToPagedList(page, rows).ToList();
                return Json(new { total = list.Count, rows = list2 });
            }

        }

        //新增优惠信息  
        //
        [HttpPost]
        public override ActionResult Save(storediscount model)
        {
            string msg = "";
            int result = 0;
            int jieguo = 1;
            if (model.storeId != 0)
            {
                model.status = 1;
                model.type = 1;//店铺折扣 1
                if (bll.Add(model) > 0)
                {
                    msg = "添加成功";
                    result = 1;
                    jieguo = 1;
                    return Json(new { jieguo = jieguo, state = result, msg = msg });
                }
                else
                {
                    jieguo = 1;
                    msg = "添加失败";
                    return Json(new { jieguo = jieguo, state = result, msg = msg });
                }

            }
            else
            {
                jieguo = 0;
                msg = "请填写完整";
                return Json(new { jieguo = jieguo, state = result, msg = msg });
            }
        }

        /// <summary>
        /// 修改 信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>

        public override ActionResult Modify(storediscount model)
        {
            string msg = "修改失败";
            string state = "false";
            model.status = 1;
            model.type = 1;
            if (model.storeId != 0)
            {
                if (bll.Modify(model) > 0)
                {
                    msg = "修改成功";
                    state = "true";
                }
            }
            else
            {
                msg = "请填写完整";
                return Json(new { state = state, msg = msg });
            }

            return Json(new { state = state, msg = msg });
        }
        public class GetOneModel
        {
            public long storeDiscountId;
            public string startTime;
            public string engTime;
            public string value;            
            public string storeName;
            public int storeId;
            public decimal? fullMoney1;
            public decimal? fullMoney2;
            public decimal? cutMoney1;
            public decimal? cutMoney2;

        }

        //获取选中的一条数据   
        public ActionResult GetOnlyOne(long id)
        {
            var getList = new List<GetOneModel>();

            var list = bll.Search(x => x.storeDiscountId == id).Select(x => new { x.storeDiscountId, x.storeId,x.value, x.store.storeName, x.startTime, x.endTime, }).ToList()[0];
            GetOneModel oneModel = new GetOneModel();
            oneModel.engTime = Convert.ToDateTime(list.endTime).ToString("yyyy-MM-dd");
            oneModel.startTime = Convert.ToDateTime(list.startTime).ToString("yyyy-MM-dd");

            oneModel.storeDiscountId = list.storeDiscountId;
            oneModel.storeName = list.storeName;
            oneModel.storeId = list.storeId;
            oneModel.value = list.value;
            getList.Add(oneModel);
            return Json(getList.ToList()[0], JsonRequestBehavior.AllowGet);
        }

        //删除信息  修改状态为
        public ActionResult ModifyState(long id)
        {
            string msg = "删除失败";
            string state = "false";
            var oneList = bll.Search(x => x.storeDiscountId == id).ToList();
            storediscount model = new storediscount();
            model.storeDiscountId = oneList.FirstOrDefault().storeDiscountId;
            model.storeId = oneList.FirstOrDefault().storeId;
            model.value = oneList.FirstOrDefault().value;
            model.startTime = oneList.FirstOrDefault().startTime;
            model.endTime = oneList.FirstOrDefault().endTime;
            model.status = 0;
            model.type = 1;
            if (bll.Modify(model) > 0)
            {
                msg = "删除成功";
                state = "true";
            }
            return Json(new { state = state, msg = msg });
        }

    }
}
